package com.cpecc.controller.wuzi;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cpecc.manager.system.DepManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.system.WuZiLBManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.Dep;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.WuZi;

@Controller
@RequestMapping(value = "/wuzi")
public class WuZiGuanLiController {

	@Autowired
	private WuZiLBManager wuZiLBManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private DepManager depManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@RequestMapping(value = "/kuCunWuZi")
	public ModelAndView kuCunWuZi() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());
		mav.setViewName("wuzigl/kuCunWuZi");
		return mav;
	}
	
	@RequestMapping(value = "/kuCunWuZiCX")
	public ModelAndView kuCunWuZiCX(WuZi wuZi,String wuZiLBId) {		
		ModelAndView mav = new ModelAndView();		
		List<WuZi> list=wuZiManager.getWuZiList(wuZi, wuZiLBId, new int[]{105, 107});
		List<WuZi> result=new ArrayList<>();
 		for(WuZi w:list){
 			if(weiXiuManager.hasWuZi(w.getId()) || yiJiaoManager.hasWuZi(w.getId()) || baoFeiManager.hasWuZi(w.getId()) || w.getShiYongZT().getId() == 107){
				w.setPanDian(1);
			} else {
				w.setPanDian(0);
			}
			result.add(w);
		}
		mav.addObject("kuCunWuZiList", result);
		mav.setViewName("wuzigl/kuCunListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/kuCunWuZiCXXQB")
	public ModelAndView kuCunWuZiCXXQB(WuZi wuZi,String wuZiLBId) {		
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuZiList", wuZiManager.getKuCunListCX(wuZi,wuZiLBId));
		mav.setViewName("wuzi/wuZiXQB");
		return mav;
	}
	
	@RequestMapping(value = "/kuCunPL")
	public void kuCunPL(HttpServletRequest request,HttpServletResponse response){
		String type[]=request.getParameterValues("kcId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/kuCunBaoFeiPL")
	public ModelAndView kuCunBaoFeiPL(HttpServletRequest request){
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("kcId");
		if(type!=null){
			String kcIds="";
			for(int i=0;i<type.length;i++){
				kcIds=kcIds+type[i]+",";
			}
			if(kcIds.length()>0)
				kcIds=kcIds.substring(0, kcIds.length()-1);
			mav.addObject("kcIds",kcIds);
		}
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String status="display";
		if(user.getRole().getRoleName().equals("部门负责人")){
			if(user.getGangWei().equals("1")){
				mav.addObject("lingDaos", userManager.getBuMenLingDaos2(user));
			}else if(user.getGangWei().equals("2")){
				status="none";
			}
		}else{
			mav.addObject("lingDaos", userManager.getBuMenLingDaos(user));	
		}
		mav.addObject("status", status);
		mav.setViewName("wuzigl/baoFeiPL");
		return mav;
	}
	
	@RequestMapping(value = "/allWuZi")
	public ModelAndView allWuZi() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("wuzigl/allWuZi");
		return mav;
	}
	
	@RequestMapping(value = "/allWuZiCX")
	public ModelAndView allWuZiCX(WuZi wuZi,String shiYongdepId,String shiYonguserId, String wuZiLBId,Integer wuZiZTId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("allWuZiList", wuZiManager.getWuZiList(wuZi,wuZiLBId,wuZiZTId,shiYongdepId,shiYonguserId));
		mav.setViewName("wuzigl/allListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/allWuZiCXXQB")
	public ModelAndView allWuZiCXXQB(WuZi wuZi,String shiYongdepId,String shiYonguserId, String wuZiLBId,int wuZiZTId) {		
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuZiList", wuZiManager.getWuZiList(wuZi,wuZiLBId,wuZiZTId,shiYongdepId,shiYonguserId));
		mav.setViewName("wuzi/wuZiXQB");
		return mav;
	}
	
	@RequestMapping(value = "/buMenList")
	public ModelAndView buMenList(){
		ModelAndView mav=new ModelAndView();		
		mav.addObject("wuZiLB1", wuZiLBManager.getAllWuZiLB1());
		mav.setViewName("wuzigl/buMenWuZi");
		return mav;
	}
	
	@RequestMapping(value = "/buMenWuZiCX")
	public ModelAndView buMenWuZiCX(WuZi wuZi,String wuZiLBId,int wuZiZTId) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Dep dep=user.getDep();
		ModelAndView mav = new ModelAndView();
		mav.addObject("buMenWuZiList", wuZiManager.getBuMenWuZiListCX(dep,wuZi,wuZiLBId,wuZiZTId));
		mav.setViewName("wuzigl/buMenWuZiListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/buMenWuZiCXXQB")
	public ModelAndView buMenWuZiCXXQB(WuZi wuZi,String wuZiLBId,int wuZiZTId) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Dep dep=user.getDep();
		ModelAndView mav = new ModelAndView();
		mav.addObject("wuZiList", wuZiManager.getBuMenWuZiListCX(dep,wuZi,wuZiLBId,wuZiZTId));
		mav.setViewName("wuzi/wuZiXQB");
		return mav;
	}
}
